package com.xinke.module.system.manager.auth;

import cn.hutool.core.collection.CollUtil;
import com.xinke.module.system.mapper.user.RoleMenuMapper;
import com.xinke.module.system.model.entity.user.MenuDO;
import com.xinke.module.system.model.entity.user.RoleMenuDO;
import com.xinke.module.system.service.user.MenuService;
import com.xinke.module.system.service.user.RoleService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;

import static com.xinke.component.common.util.collection.CollectionUtils.convertSet;

/**
 * @Author yzx
 * @CreateTime 2025/3/30 12:40
 * @Version 1.0
 * @Description:
 */
@Component
public class PermissionManager {

    @Resource
    private RoleService roleService;

    @Resource
    private MenuService menuService;

    @Resource
    private RoleMenuMapper roleMenuMapper;
    /**
     * 获得角色们拥有的菜单编号集合
     *
     * @param roleIds 角色编号数组
     * @return 菜单编号集合
     */
    public Set<Long> getRoleMenuListByRoleId(Collection<Long> roleIds) {
        if (CollUtil.isEmpty(roleIds)) {
            return Collections.emptySet();
        }

        // 如果是管理员的情况下，获取全部菜单编号
        if (roleService.hasAnySuperAdmin(roleIds)) {
            return convertSet(menuService.getMenuList(), MenuDO::getMenuId);
        }
        // 如果是非管理员的情况下，获得拥有的菜单编号
        return convertSet(roleMenuMapper.selectListByRoleId(roleIds), RoleMenuDO::getMenuId);
    }
}
